#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct {
    char name[51];              
    int time_to_prepare;       
    int max_capacity;           
    int current_capacity;       
} Food;

typedef struct {
    char name[51];              
    char foods[5][51];          
    int num_foods;              
} Combo;

typedef struct {
    int hour;                   
    int minute;                 
    int second;                 
    char item[5][51];           
    int is_combo;     
    int is_handle;          
} Order;

typedef struct {
    char food_name[51];         
    int remaining_time;         
} Pending;

Food foods[100];
Pending pending[100];
Combo combos[100];
int W1, W2;
int pending_count = 0;

void initialize(int N, int M) {
    for (int i = 0; i < N; i++) {
        foods[i].current_capacity = 0;  
    }
    for (int i = 0; i < M; i++) {
        combos[i].num_foods = 0;        
    }

}

void load_menu(FILE *menu) {
    int N, M;
    fscanf(menu, "%d %d", &N, &M);
    for (int i = 0; i < N; i++) {
        fscanf(menu, "%s", foods[i].name);
    }
    for (int i = 0; i < N; i++) {
        fscanf(menu, "%d", &foods[i].time_to_prepare);
    }
    for (int i = 0; i < N; i++) {
        fscanf(menu, "%d", &foods[i].max_capacity);
    }
    fscanf(menu, "%d %d", &W1, &W2);
    char buffer[1000];
    fgets(buffer, sizeof(buffer), menu);
    for (int i = 0; i < M; i++) {
        fgets(buffer, sizeof(buffer), menu);
        sscanf(buffer, "%s", combos[i].name);
        char *token = strtok(buffer, " ");
        int j = 0;
        while ((token = strtok(NULL, " ")) != NULL) {
            if (token[strlen(token) - 1] == '\n') {
                token[strlen(token) - 1] = '\0';
            }
            strcpy(combos[i].foods[j], token);
            combos[i].num_foods++;
            j++;
        }
        while(1){
        	if(j==5){
        		break;
			}
        	combos[i].foods[j][0] = '\0';
        	j++;
		}
    }
    fclose(menu);
}

int find_food_time(char *food_name) {
    for (int i = 0; i < 100; i++) {
        if (strcmp(food_name, foods[i].name) == 0) {
            return foods[i].time_to_prepare;
        }
    }
    return -1;
}

int get_processing_orders_count(Order *orders, int n) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (orders[i].is_handle == 1) {
            count++;
        }
    }
    return count;
}

void process_orders(Order *orders, int n) {
    for (int i = 0; i < 100; i++) {
        foods[i].current_capacity = foods[i].max_capacity;
    }
    int current_hour = 7, current_minute = 0, current_second = 0;
    int order_index = 0;
    while (current_hour <= 22 && order_index < n) {
        char item[n][20];
        while (order_index < n && orders[order_index].hour <= current_hour &&
               orders[order_index].minute <= current_minute &&
               orders[order_index].second <= current_second) {
            int handle = 0;
            for (int j = 0; j < 5; j++) {
                if (strcmp(orders[order_index].item[j], "") != 0) {
                    for (int k = 0; k < 100; k++) {
                        if (!orders[order_index].is_combo) {
                            strcpy(item[order_index], orders[order_index].item[0]);
                        }
                        if (strcmp(orders[order_index].item[j], foods[k].name) == 0) {
                            if (foods[k].current_capacity > 0) {
                                orders[order_index].is_handle = 1;
                                handle = 1;
                                foods[k].current_capacity--;
                                strcpy(pending[pending_count].food_name, foods[k].name);
                                pending[pending_count].remaining_time = foods[k].time_to_prepare;
                                orders[order_index].item[j][0] = '\0';
                                pending_count++;
                                break;
                            }
                        }
                    }
                }
            }
            if (handle) {
                for (int i = 0; i < n; i++) {
                    int flag = 1;
                    for (int j = 0; j < 5; j++) {
                        if (strcmp(orders[order_index].item[j], "") != 0) {
                            flag = 0;
                            break;
                        }
                    }
                    if (flag == 1) {
                        if (orders[order_index].is_combo) {
                            printf("%02d:%02d:%02d\n", current_hour, current_minute, current_second);
                        } else {
                            printf("%02d:%02d:%02d\n", current_hour, current_minute,
                                   current_second + find_food_time(item[order_index]));
                        }
                        orders[order_index].is_handle = 0;
                        strcpy(orders[order_index].item[0], "已完成");
                    }
                }
            }
            order_index++;
        }
        order_index = 0;
        current_second++;
        if (current_second == 60) {
            current_second = 0;
            current_minute++;
        }
        if (current_minute == 60) {
            current_minute = 0;
            current_hour++;
        }
        for (int i = 0; i < pending_count; i++) {
            if (pending[i].remaining_time == 0) {
                for (int j = 0; j < 100; j++) {
                    if (strcmp(pending[i].food_name, foods[j].name) == 0) {
                        pending[i].remaining_time = -1;
                        foods[j].current_capacity++;
                        break;
                    }
                }
            } else if (pending[i].remaining_time != -1) {
                pending[i].remaining_time--;
                if (pending[i].remaining_time == 0) {
                    for (int j = 0; j < 100; j++) {
                        if (strcmp(pending[i].food_name, foods[j].name) == 0) {
                            pending[i].remaining_time = -1;
                            foods[j].current_capacity++;
                            break;
                        }
                    }
                }
            }
        }
		sleep(1);
    }
}

int main() {
    FILE *menu_file = fopen("dict.dic", "r");
    if (menu_file == NULL) {
        fprintf(stderr, "Could not open menu file.\n");
        return 1;
    }
    int n;
    scanf("%d", &n);
    Order orders[n];
    initialize(100, 100);
    load_menu(menu_file);
    char food_name[51];
    for (int i = 0; i < n; i++) {
        scanf("%d:%d:%d %s", &orders[i].hour, &orders[i].minute, &orders[i].second, food_name);
        int found = 0;
        int count = 0;
        for (int j = 0; j < 100; j++) {
            if (strcmp(food_name, combos[j].name) == 0) {
                for (int k = 0; k < combos[j].num_foods; k++) {
                    strcpy(orders[i].item[k], combos[j].foods[k]);
                    count++;
                }
                found = 1;
                orders[i].is_combo = 1;
                break;
            }
        }
        if(count != 5 && found== 1){
        	while(1){
        		orders[i].item[count][0] = '\0';
        		count++;
        		if(count==5){
        			break;
				}
			}
		}
        if (!found) {
            orders[i].is_combo = 0;
            for (int j = 0; j < 100; j++) {
                if (strcmp(food_name, foods[j].name) == 0) {
                    strcpy(orders[i].item[0], food_name);
                }
            }
            for(int j = 1;j < 5;j++){
        	orders[i].item[j][0] = '\0';
		}
		
        }
        
    }
    process_orders(orders, n);
    fclose(menu_file);
    return 0;
}

